package com.softer.wxzj.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.softer.wxzj.common.entity.CommonResult;
import com.softer.wxzj.common.entity.Page;
import com.softer.wxzj.common.enums.ShiroResultEnum;
import com.softer.wxzj.common.util.ShiroUserUtil;
import com.softer.wxzj.entity.DicType;
import com.softer.wxzj.mapper.DicContentMapper;
import com.softer.wxzj.mapper.DicTypeMapper;
import com.softer.wxzj.service.DicTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(rollbackFor = { RuntimeException.class, Exception.class })
public class DicTypeServiceImpl implements DicTypeService {
    @Autowired
    private DicContentMapper diccontentMapper;
    @Autowired
    private DicTypeMapper dictypeMapper;
    @Override
    public CommonResult insert(DicType dicType) {
        dicType.setCreater(ShiroUserUtil.getUserName());
        try {

            int a= dictypeMapper.insert(dicType);
            return CommonResult.ResponseResult(a);
        }catch  (DuplicateKeyException a){
            return new CommonResult(ShiroResultEnum.FAIL.getCode(), "类型已被使用");
        }
    }
    @Override
    public CommonResult update(DicType dictype) {
        dictype.setCreater( ShiroUserUtil.getUserName());
        int i = dictypeMapper.update(dictype);
        return CommonResult.ResponseResult(i);
    }
    @Override
    public CommonResult delete(String id) {
        //包含流程的类型不可删
        int i=diccontentMapper.selectCountByCode(id);
        if(i!=0){
                return new CommonResult(ShiroResultEnum.FAIL.getCode(), "流程类型已被使用");
            }
        int m= dictypeMapper.delete(id);
        return CommonResult.ResponseResult(m);
    }
    @Override
    public PageInfo<DicType> getList(Page page, DicType dictype) {
        //只有紧跟在PageHelper.startPage方法后的第一个Mybatis的查询（Select）方法会被分页。
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        List<DicType> list = dictypeMapper.getList(dictype);
        PageInfo<DicType> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }
    @Override
    public DicType getInfo(String id) {
        return dictypeMapper.getInfo(id);
    }

    @Override
    public  PageInfo<DicType> fuzzySearch(Page page,String fuzzy) {
        //只有紧跟在PageHelper.startPage方法后的第一个Mybatis的查询（Select）方法会被分页。
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        List<DicType> list = dictypeMapper.fuzzySearch(fuzzy);
        PageInfo<DicType> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }
}
